package com.zgc.view;

import com.fx.spring.util.ScreenInfoUtil;
import com.zgc.util.LocalDataUtil;
import com.zgc.view.controller.RootBorderCtrl;
import com.fx.spring.annotation.StartFX;
import com.fx.spring.jfx.AbstractSpringStartFX;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 初始化spring环境
 */
@StartFX
public class ZookeeperStageFX extends AbstractSpringStartFX {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RootBorderCtrl rootBorderCtrl;


    @Override
    protected void init() throws Exception {
        super.init();
        LocalDataUtil.connectInfoReaderLocal();
        LocalDataUtil.connectInfoWatch();
    }

    @Override
    public void start(Stage primaryStage) {
        logger.debug("{}主窗口加载开始。。", primaryStage.toString());

        double width = ScreenInfoUtil.VISUAL_WIDTH * 0.7;
        double height = ScreenInfoUtil.VISUAL_HEIGHT * 0.7;
        // 根节点使用 StackPane
        StackPane rootPane = new StackPane();
        Scene scene = new Scene(rootPane, width, height);
        scene.getStylesheets().addAll("view/css/Common.css");
        logger.debug("[{}]文件加载", scene.getStylesheets());
        primaryStage.setScene(scene);
        primaryStage.getIcons().add(new Image("view/images/icon.png"));
        logger.debug("[{}]图标加载", primaryStage.getIcons().get(0).getUrl());
        primaryStage.setTitle("ZookeeperUI");
        primaryStage.show();
        //绑定窗口大小
        rootPane.prefWidthProperty().bind(primaryStage.widthProperty());
        rootPane.prefHeightProperty().bind(primaryStage.heightProperty());
        // 异步加载组件
        Platform.runLater(() -> rootPane.getChildren().add(rootBorderCtrl.load()));

        logger.debug("[{}]主窗口加载结束。。", primaryStage.getTitle());
    }


    @Override
    protected void stop() throws Exception {
        super.stop();
        LocalDataUtil.connectInfoWriteLocal();
    }
}
